/*

Program programs.do defines auxiliary programs that will subsequently be called in other do-files.

Eric Verhoogen

 Kumler, Verhoogen, Frias "Enlisting Employees ..." REStat forthcoming

*/

#delimit;
set more off;
set logtype text;
set linesize 120;
clear;


***************************************************************************;
***************************************************************************;
********************** programs for making tables *************************;
***************************************************************************;
***************************************************************************;
***************************************************************************;

*** Program initializetable sets up tables to be fed automatically into latex;

capture program drop initializetable;
program define initializetable;

* arguments:
`1' - table name (i.e. tsumstats19932001)
`2' - number of columns
`3' - number of parameters
`4' - number of other statistics
`5' - number of rows of dummies
`6' - number of first unused column
`7' - number of second unused column
`8' - number of third unused column
`9' - labels of rows, corresponding to parameters
`10' - labels of rows, corresponding to other statistics
;

** set dimensions;
local `1'_cols = `2';
local `1'_nb = `3'; * number of parameters to be reported;
local `1'_ne = `4'; * number of other statistics to be reported;
local `1'_nd = `5'; * number of rows of dummy variable indicators (Y/N);

** initialize matrices;
matrix hold_b = J(``1'_nb', ``1'_cols', .);
matrix hold_se = J(``1'_nb', ``1'_cols', .);
matrix hold_stars = J(``1'_nb', ``1'_cols', .);
matrix hold_e = J(``1'_ne', ``1'_cols', .);
if ``1'_nd'> 0
{;
 matrix hold_d = J(``1'_nd', ``1'_cols', .);
};

drop _all;
sh echo "`9'">${tmp}`1'_rowlabels_b.raw;
infile str64 rowlabel using ${tmp}`1'_rowlabels_b.raw;
gen row = _n*2-1;
sort row;
save ${tmp}`1'_rowlabels_b.dta, replace;  

drop _all;
sh echo " `10' ">${tmp}`1'_rowlabels_e.raw;
infile str64 rowlabel using ${tmp}`1'_rowlabels_e.raw;
gen row = _n;
sort row;
save ${tmp}`1'_rowlabels_e.dta, replace;

** make datasets to hold indicators for dummy variables;
* no dummies in this case, but still need to create dataset;

drop _all;
set obs `5'; * this should be sufficient -- never more than 5 set of dummies;
forvalues i = 1/`2'
{;
 gen str12 col`i's = "";
};
save ${tmp}`1'_dummies.dta, replace;

end;


************** genstars ***************************;

*** program to put stars on estimated coefficients, used by mat2table below;

capture program drop genstars;
program define genstars, rclass;
* inputs:
  1. estimate
  2. standard error
  3. 1-star level (i.e. 10%)
  4. 2-star level (i.e. 5%)
  5. 3-star level (i.e. 1%)
;

local stars=0;
if ttail(e(N)-e(df_m),abs(`1'/`2')) < `5'/2
{;
 local stars=3;
};
else if ttail(e(N)-e(df_m),abs(`1'/`2')) < `4'/2
{;
 local stars=2;
};
else if ttail(e(N)-e(df_m),abs(`1'/`2')) < `3'/2
{;
 local stars=1;
};
return scalar nstars = `stars';
end;

***************** mat2table *****************************;

*** program to take information from matrics hold_b, hold_se, hold_e and hold_stars
*** and turn it into a table in latex;

capture program drop mat2table;
program define mat2table;

* inputs:
  1. number of columns
  2. number of coeff ests per column
  3. name of table (i.e. "tplantout")
  4. format for estimates (i.e. "%12.3f")
  5. format for std errors (i.e. "%12.3f")
  6. format for first row of regression stats
  7. format for second row of regression stats
  8. format for third row of regression stats
  9. number of rows of dummy variables
  ;

version 9.0;

* convert matrix of estimates to dataset of string variables;
drop _all; * drops dataset in memory to make room for dataset of estimates;
svmat double hold_b, names(column);
svmat double hold_stars, names(starcol);

* change format and add stars to coefficient estimates;
forvalues i = 1/`1'
{;
 gen str3 starcol_`i' = "***" if starcol`i'==3;
 replace starcol_`i' = "**" if starcol`i'==2;
 replace starcol_`i' = "*" if starcol`i'==1;
 gen str12 col`i's = string(column`i', "`4'") if column`i'~=.;
 replace col`i's = col`i's + starcol_`i' if col`i's~="";
};
gen row = _n*2-1;
drop column*;
tempfile hold_b;
save `hold_b', replace;

** convert matrix of std errors to dataset of string variables;
drop _all; * drops dataset in memory to make room for dataset of estimates;
svmat double hold_se, names(column);
* change format and add parentheses;
forvalues i = 1/`1'
{;
 gen str12 col`i's = "(" + string(column`i', "`5'") + ")" if column`i'~=.;
};
gen row = _n*2;
drop column*;
tempfile hold_se;
save `hold_se', replace;

** combine estimates and std errors;
drop _all;
use `hold_b';
append using `hold_se';
sort row;

** merge in row labels (i.e. for independent variables);
merge row using ${tmp}`3'_rowlabels_b.dta;
drop _merge;
order rowlabel col*;

** write to a tex file;
display "writing to tex file `3'_b.tex";
listtex rowlabel col* using ${tables}`3'_b.tex, rstyle(tabular) replace; 

list rowlabel col*;

** make tex file of other regression statistics;
drop _all;
svmat double hold_e, names(column);
forvalues i = 1/`1'
{;
 gen str12 col`i's = string(column`i', "`6'") if _n==1 & column`i'~=.;
 replace col`i's = string(column`i', "`7'") if _n==2 & column`i'~=.;
 replace col`i's = string(column`i', "`8'") if _n==3 & column`i'~=.;
};
tempfile hold_e;
save `hold_e', replace;

** convert 0/1 matrix for dummy variables to N/Y;
drop _all;
use ${tmp}`3'_dummies.dta;
forvalues i = 1/`1'
{;
 forvalues j = 1/`9'
 {;
  replace col`i's = "Y" if _n==`j' & hold_d[`j',`i']==1;
  replace col`i's = "N" if _n==`j' & hold_d[`j',`i']==0;  
  replace col`i's = " " if _n==`j' & hold_d[`j',`i']==.;
};
};

append using `hold_e';
gen row=_n;
sort row;
merge 1:1 row using ${tmp}`3'_rowlabels_e.dta;
drop _merge column*;
order rowlabel col*;

** write to a tex file;
display "writing to tex file `3'_e.tex";
listtex rowlabel col* using ${tables}`3'_e.tex, rstyle(tabular) replace; 

list rowlabel col*;
  
end;


